//
//  ConsultViewController.m
//  soccer
//
//  Created by 上官千城 on 15/9/2.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "ConsultViewController.h"

@interface ConsultViewController ()

@end

@implementation ConsultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"资讯";
    
    
    
    [self afnGetRequestComment];
    [self afnGetRequest];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, WIDTH, HEIGHT-46) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //取消换航线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
//    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"cookie" ]);
    //评论详情表
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    self.backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.backView];
    
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, HEIGHT)];
    aView.backgroundColor = [UIColor clearColor];
    [self.backView addSubview:aView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackView:)];
    [aView addGestureRecognizer:tap];
    
    self.commentTableView = [[UITableView alloc]initWithFrame:CGRectMake(60, -20, WIDTH - 60, HEIGHT) style:UITableViewStyleGrouped];
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    self.backView.layer.shadowOffset = CGSizeMake(0, 3);
    self.backView.layer.shadowOpacity = 0.5;
    self.backView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.backView.layer.shadowRadius = 5.0;
    //    self.commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.backView addSubview:self.commentTableView];
    //滑动手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
    //    swipe.delegate = self;
    [self.commentTableView addGestureRecognizer:swipe];
    
    
    
}

//滑动评论详情回滚返回主页
-(void)swipeAction
{
    [UIView animateWithDuration:0.3f animations:^{
        self.tableView.frame = CGRectMake(0, -20, WIDTH, HEIGHT);
        
        self.backView.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
        
    }];
}

//点击评论详情回滚返回主页
-(void)tapBackView:(UITapGestureRecognizer *)sender
{
    
    
    [UIView animateWithDuration:0.3f animations:^{
        self.tableView.frame = CGRectMake(0, -20, WIDTH, HEIGHT);
        
        self.backView.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
        
    }];
}


//tableview协议方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        if ( indexPath.row == 0) {
            static NSString *info = @"consultCell";
            ConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:info];
            if (cell == nil) {
                cell = [[ConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:info];
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        Consult *con = self.indexArray[indexPath.section][indexPath.row];
        cell.con = con;
        cell.myDelgate = self;
        cell.imgDelegate1 = self;
        return cell;
    }
    if (indexPath.row == 1) {
        static NSString *cellInfo = @"CommentCell";
        ConsultCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellInfo];
        if (cell1 == nil) {
            cell1 = [[ConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfo];
        }
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
//        NSLog(@"%@",self.comArray);
        if (self.comArray.count != 0) {
            concom *com = self.comArray[indexPath.section];
            cell1.com = com;
        }
        cell1.myDelegate = self;
        return cell1;
        }
    }
    if (tableView == self.commentTableView) {
        static NSString *cellinfo = @"RightCommentCell";
        ConsultCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellinfo];
        if (cell2 == nil) {
            cell2 = [[ConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellinfo];
        }
        if (self.comArray.count != 0) {
            concom *com = self.comArray[indexPath.section];
            cell2.com = com;
        }
        return cell2;
    }

    return nil;
}
//点击Cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.tableView) {
    if (indexPath.row == 0) {
        consultDetailsController *view = [[consultDetailsController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
        //隐藏tabelbar & 隐藏nav
//        view.hidesBottomBarWhenPushed = YES;
//        view.navigationController.navigationBarHidden = YES;
    }
    if (indexPath.row == 1) {
        [UIView animateWithDuration:0.3f animations:^{
            self.tableView.frame = CGRectMake(-60, -20, WIDTH, HEIGHT);
            
            self.backView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
            
        }];
        
        }
    }
    if (tableView == self.commentTableView) {
        NSLog(@"点击方法");
    }

}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
//区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView) {
        return self.array.count;
    }
    return 1;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        if (indexPath.row == 0) {
            return 200;
        }
        return 500;
    }
    if (tableView == self.commentTableView) {
        
    }
    return 500;
}


//请求cell2数据
-(void)afnGetRequestComment
{
    self.comArray = [NSMutableArray array];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://mrobot.pcbaby.com.cn/v2/qzbd/columnDetail?articleId=2370764&expertId=29003920&num=39&pageNo=1&req_enc=utf-8&resp_enc=utf-8";
    //http://192.168.1.43:9099/OServer/TestServlet
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        //        [[DateHandel shareHandle]dropTable];
        //        [[DateHandel shareHandle]createTable];
        //        [self.array removeAllObjects];
//        NSDictionary *fields = [operation.response allHeaderFields]; //afnetworking写法
//        NSLog(@"fields = %@",[fields description]);
//        NSURL *url = [NSURL URLWithString:@"http://api.skyfox.org/api-test.php"];
//        //获取cookie方法1
//        NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:fields forURL:url];
//        
//        NSLog(@"cookies = %@",cookies);
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            concom *rea = [[concom alloc]init];
            rea.accountName = [dic objectForKey:@"accountName"];
            rea.answerIntro = [dic objectForKey:@"answerDetails"];
            rea.answerAccountPic = [dic objectForKey:@"answerAccountPic"];
            rea.title = [dic objectForKey:@"title"];
            rea.questionId = [dic objectForKey:@"questionId"];
            rea.accountId = [dic objectForKey:@"accountId"];
            [self.comArray addObject:rea];
        }
        

        
        [self.tableView reloadData];
        //        [self.HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        //        [self.HUD hide:YES];
        
        
    }];
    
}

//请求cell1 数据
-(void)afnGetRequest
{
    
    self.array = [NSMutableArray array];
    self.indexArray = [NSMutableArray array];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://mrobot.pcbaby.com.cn/v2/qzbd/cytList?type=ask&pageNo=1";
    
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        //        [[DateHandel shareHandle]dropTable];
        //        [[DateHandel shareHandle]createTable];
        //        [self.array removeAllObjects];
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"data"];
       
        for (NSMutableDictionary *dic in array) {
            Consult *rea = [[Consult alloc]init];
            rea.image = [dic objectForKey:@"image"];
            rea.articleId = [dic objectForKey:@"articleId"];
            rea.date = [dic objectForKey:@"date"];
            rea.title = [dic objectForKey:@"title"];
            rea.expertId = [dic objectForKey:@"expertId"];
            rea.num = [dic objectForKey:@"num"];
            rea.preview = [dic objectForKey:@"preview"];
            [self.array addObject:rea];
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObject:[self.array lastObject]];
            [self.indexArray addObject:arr];
//            NSLog(@"arr = %@ ",self.array);
        }
//        NSLog(@"%@",self.indexArray);
        [self.tableView reloadData];
        //        [self.HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        //        [self.HUD hide:YES];
        
        
    }];
    
}




-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.frame = CGRectMake(0, HEIGHT - 49, WIDTH, 49);
    [self.tableView reloadData];
    
    Preference *pre = [[[Account shareHandle]selectAllPreference]lastObject];
    //显示图片数
    pre.photoNum = self.photoNum;
    pre.consultSwitch = self.consultSwitch;
    pre.like = self.like;
    pre.commentNum = self.commentNum;
    pre.accept = self.accept;
    
    

}

//点击图片传对象
-(void)bringIm1:(Consult *)con
{
    PhotoAlbumController *view = [[PhotoAlbumController alloc]init];
    view.con = con;
    [self.navigationController pushViewController:view animated:YES];
}


//协议方法  push到个人页面
-(void)pushPerson
{
    PersonViewController *view = [[PersonViewController alloc]init];
    //下一页面隐藏tabbar  必须在上一级页面的点击时间里写
    view.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:view animated:YES];
   
}
//协议方法点击媒体头像 push 媒体
-(void)pushMP4
{
    NSLog(@"mp4");
    MainCommentController *view = [[MainCommentController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
}



@end
